* Updated version that includes those with IRMF only
clear

local yr = `1'
local adv = "`2'"

/* Define globals */
* Columns from f8962
global columnA "e85622 e85634 e85646 e85658 e85670 e85682 e85694 e85706 e85718 e85730 e85742 e85754"
global columnB "e85624 e85636 e85648 e85660 e85672 e85684 e85696 e85708 e85720 e85732 e85744 e85756"
global columnC "e85626 e85638 e85650 e85662 e85674 e85686 e85698 e85710 e85722 e85734 e85746 e85758"
global columnD "e85628 e85640 e85652 e85664 e85676 e85688 e85700 e85712 e85724 e85736 e85748 e85760"
global columnE "e85630 e85642 e85654 e85666 e85678 e85690 e85702 e85714 e85726 e85738 e85750 e85762"
global columnF "e85632 e85644 e85656 e85668 e85680 e85692 e85704 e85716 e85728 e85740 e85752 e85764"

* Columns from f1095A
global columnA_f1095A "ew901 ew902 ew903 ew904 ew905 ew906 ew907 ew908 ew909 ew910 ew911 ew912"
global columnB_f1095A "ew921 ew922 ew923 ew924 ew925 ew926 ew927 ew928 ew929 ew930 ew931 ew932"
global columnC_f1095A "ew941 ew942 ew943 ew944 ew945 ew946 ew947 ew948 ew949 ew950 ew951 ew952"

* Import parameters for calculating applicable figure and poverty levels
import excel using "$dataDir/f8962Params.xlsx", firstrow sheet("af")
save "$dataDir/af.dta", replace
clear
import excel using "$dataDir/f8962Params.xlsx", firstrow sheet("fpl")
save "$dataDir/fpl.dta", replace
clear
import excel using "$dataDir/f8962Params.xlsx", firstrow sheet("repay")
save "$dataDir/repay.dta", replace
clear

use "$dataDir/soi`yr'`adv'.dta", clear

rename rec_id recid
rename fedpovcd povTable
rename e85580 state_f8962
rename s002 issn

rename e00100 agi
rename e02650 totinc
rename e02900 ttadj
rename e02400 ssinc 	// ss benefits
rename e02500 ssagi 	// taxable ss benefits
rename e02700 forexc 
rename e00400 texint 	// tax exempt interest
rename e85570 agi_kid_file
rename e85565 magi
rename e85575 exch_income_f8962

gen wt = s006
gen filer = 1

if(`yr' > 17) local nump "numpeople"

keep recid povTable state_f8962 s0* wt dsi flpdyr e85610 e85612 e85614 e85616 e85618 e85620 $columnA $columnB $columnE $columnF ptcind xtot8962 f8962 e85770 e85775 e85780 e85785 e85790 e85795 hincpl mars n2 `nump' cycle issn filer agi totinc ttadj ssinc ssagi forexc texint agi_kid_file magi exch_income_f8962 e02300

gen stateFPL = .
replace stateFPL = 1 if povTable == "C" /* 48 states + DC */
replace stateFPL = 2 if povTable == "A" /* Alaska */
replace stateFPL = 3 if povTable == "B" /* Hawaii */

gen annPrem = e85610
gen annSLCSP = e85612
gen annPTC = e85618
gen annAPTC = e85620
replace annPrem = 0 if annPrem == .
replace annSLCSP = 0 if annSLCSP == .
replace annAPTC = 0 if annAPTC == .
replace annPTC = 0 if annPTC == .

gen numMoPrem = 0
gen numMoSLCSP = 0
gen numMoAPTC = 0
gen numMoPTC = 0
gen numMoExch = 0
replace numMoPrem = 12 if ptcind == 1
replace numMoSLCSP = 12 if ptcind == 1
replace numMoAPTC = 12 if ptcind == 1
replace numMoPTC = 12 if ptcind == 1
replace numMoExch = 12 if ptcind == 1

forvalues i = 1/12 {
	local prem = 85622 + (`i' - 1) * 12
	local slcsp = `prem' + 2
	local ptc = `prem' + 8
	local aptc = `prem' + 10
	
	gen moPrem_`i' = e`prem'
	gen moSLCSP_`i' = e`slcsp'
	gen moAPTC_`i' = e`aptc'
	gen moPTC_`i' = e`ptc'
	replace moPrem_`i' = 0 if moPrem_`i' == .
	replace moSLCSP_`i' = 0 if moSLCSP_`i' == .
	replace moAPTC_`i' = 0 if moAPTC_`i' == .
	replace moPTC_`i' = 0 if moPTC_`i' == .
	
	replace numMoPrem = numMoPrem + 1 if moPrem_`i' > 0 & ptcind != 1
	replace numMoSLCSP = numMoSLCSP + 1 if moSLCSP_`i' > 0 & ptcind != 1
	replace numMoAPTC = numMoAPTC + 1 if moAPTC_`i' > 0 & ptcind != 1
	replace numMoPTC = numMoPTC + 1 if moPTC_`i' > 0 & ptcind != 1
}

* Calculate total amounts for those with partial year coverage
egen moPrem_t = rowtotal(moPrem_*)
egen moSLCSP_t = rowtotal(moSLCSP_*)
egen moAPTC_t = rowtotal(moAPTC_*)
egen moPTC_t = rowtotal(moPTC_*)
replace moPrem_t = 0 if moPrem_t == .
replace moSLCSP_t = 0 if moSLCSP_t == .
replace moAPTC_t = 0 if moAPTC_t == .
replace moPTC_t = 0 if moPTC_t == .

* f8962 totals amounts
rename e85770 PTC
rename e85775 APTC
rename e85780 trueUp
rename e85785 excessAPTC
rename e85790 repayLimit
rename e85795 APTC_repay

gen Prem = max(annPrem, moPrem_t)
replace APTC = max(APTC, annAPTC, moAPTC_t)
replace PTC = max(PTC, annPTC, moPTC_t)
gen SLCSP = max(annSLCSP, moSLCSP_t)

gen famSize = .
if(`yr' < 18) {
	replace famSize = n2
}
else {
	replace famSize = numpeople
}
replace famSize = xtot8962 if famSize == 0 & xtot8962 > 0

gen fte_f8962 = (famSize * numMoPrem) / 12

gen soiYr = 20`yr'

capture drop temp
gen temp = string(recid) + string(soiYr) + string(flpdyr)
destring temp, gen(uID_soi)
drop temp

save "$dataDir/filers_all_`yr'`adv'.dta", replace
preserve
keep if soiYr == flpdyr
gen timelyFiler = 1
save "$dataDir/filers_timely_`yr'`adv'.dta", replace
restore

keep if flpdyr < soiYr & flpdyr > 2013
gen timelyFiler = 0
save "$dataDir/filers_late_`yr'`adv'.dta", replace

keep if f8962 == 1
save "$dataDir/f8962_only_`yr'`adv'.dta", replace
